#import "RootNavigationController.h"
#import "Common.h"
NSString *const RootNavigationDidPopViewControllerNotification = @"RootNavigationDidPopViewControllerNotification";
static BOOL RootNavigationControllerItemIsBackTtileExist;
@interface RootNavigationController ()
@end
@implementation RootNavigationController
+(void)load {
    RootNavigationControllerItemIsBackTtileExist = [Common isVariableWithClass:[UINavigationItem class] varName:@"backButtonTitle"];
}
-(instancetype)initWithRootViewController:(UIViewController *)rootViewController {
    self = [super initWithRootViewController:rootViewController];
    if(self) {
        [self configChildControllerBackTitle:@[rootViewController]];
    }
    return self;
}
-(void)viewDidLoad {
    [super viewDidLoad];
    [self defaultNavigationBarStyle];
}
- (void) defaultNavigationBarStyle {
    self.navigationBar.barStyle = UIBarStyleDefault;
    self.navigationBar.translucent = NO;
//    self.navigationBar.barTintColor = ThemeMaoColor;
    self.navigationBar.titleTextAttributes = @{NSFontAttributeName : [UIFont systemFontOfSize:18], NSForegroundColorAttributeName : [UIColor blackColor]};
    UIImage *backImageOriginal = [UIImage imageNamed:@"BackArrow"];
    UIImage *backImage = [[self image:backImageOriginal leftOffset:ApplicationNavigationBarBackGauge] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [self.navigationBar setBackIndicatorImage:backImage];
    [self.navigationBar setBackIndicatorTransitionMaskImage:backImage];
    if([self respondsToSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:)]){
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    [self.navigationBar setBackgroundImage:nil forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
    self.navigationBar.shadowImage = nil;
}
- (void) setBackgroundColor:(UIColor*)color {
    UIImage *image = [self imageWithFrame:CGRectMake(0, 0, ScreenWidthMao, 3) color:color];
    [self.navigationBar setBackgroundImage:image forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
    [self.navigationBar setShadowImage:[[UIImage alloc] init]];
}
- (UIImage*) imageWithFrame:(CGRect)frame color:(UIColor*)color {
    UIGraphicsBeginImageContext(frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, color.CGColor);
    CGContextFillRect(context, frame);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
- (UIImage *)image:(UIImage*)image leftOffset:(CGFloat)leftOffset {
    UIImage *sourceImage = image;
    UIImage *newImage = nil;
    CGSize size = CGSizeMake(image.size.width + leftOffset, image.size.height);
    UIGraphicsBeginImageContextWithOptions(size, NO, [[UIScreen mainScreen] scale]);
    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = CGPointMake(leftOffset, 0);
    thumbnailRect.size.width  = size.width - leftOffset;
    thumbnailRect.size.height = size.height;
    [sourceImage drawInRect:thumbnailRect];
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage ;
}
-(void)setViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers {
    [self configChildControllerBackTitle:viewControllers];
    if(viewControllers && viewControllers.count > 1) {
        for (int i = 1; i < viewControllers.count; i++) {
            viewControllers[i].hidesBottomBarWhenPushed = YES;
        }
    }
    [super setViewControllers:viewControllers];
}
-(void)setViewControllers:(NSArray<UIViewController *> *)viewControllers animated:(BOOL)animated {
    [self configChildControllerBackTitle:viewControllers];
    if(viewControllers && viewControllers.count > 1) {
        for (int i = 1; i < viewControllers.count; i++) {
            viewControllers[i].hidesBottomBarWhenPushed = YES;
        }
    }
    [super setViewControllers:viewControllers animated:animated];
}
#pragma -mark push
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    [self configChildControllerBackTitle:@[viewController]];
    if(self.viewControllers.count > 0) {
        viewController.hidesBottomBarWhenPushed = YES;
    }
    [super pushViewController:viewController animated:animated];
}
#pragma -mark show
-(void)showViewController:(UIViewController *)vc sender:(id)sender {
    [self configChildControllerBackTitle:@[vc]];
    if(self.viewControllers.count > 0) {
        vc.hidesBottomBarWhenPushed = YES;
    }
    [super showViewController:vc sender:sender];
}
#pragma -mark pop
-(UIViewController *)popViewControllerAnimated:(BOOL)animated {
    UIViewController *controller  = [super popViewControllerAnimated:animated];
    return controller;
}
-(void)configChildControllerBackTitle:(NSArray<__kindof UIViewController*> *) viewControllers {
    if(!viewControllers || viewControllers.count == 0) {
        return;
    }
    if(RootNavigationControllerItemIsBackTtileExist) {
        for (UIViewController *controller in viewControllers) {
            [controller.navigationItem setValue:@"" forKey:@"backButtonTitle"];
        }
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
